package com.ruoyi.system.service.impl;


import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;
import com.ruoyi.system.domain.RelatedTodoevent;
import com.ruoyi.system.domain.SysDeviceState;
import com.ruoyi.system.domain.SysDeviceStateFile;
import com.ruoyi.system.domain.SysTodoevent;
import com.ruoyi.system.mapper.SysDeviceStateCheckMapper;
import com.ruoyi.system.service.*;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.jar.JarEntry;


@Service
public class SysDeviceStateCheckServiceImpl implements ISysDeviceStateCheckService {

    @Autowired
    private SysDeviceStateCheckMapper sysDeviceStateCheckMapper;
    @Autowired
    private IRelatedTodoeventService relatedTodoeventService;
    @Autowired
    private ISysTodoeventService sysTodoeventService;


    @Autowired
    private ILifManageListService lifManageListService;

    @Override
    public int updateYesState(Long devId) {

        //修改清单中技术规格说明说状态
        Long device = sysDeviceStateCheckMapper.selectFilId(devId);
        lifManageListService.updateLifManageListByDeviceId(device);
        //修改设备状态确定状态：审核通过
        //更新相关待办事项
        RelatedTodoevent relatedTodoevent = relatedTodoeventService.selectRelatedTodoeventByDevId(device);
        if(null!=relatedTodoevent){
            SysTodoevent sysTodoevent = sysTodoeventService.selectSysTodoeventById(relatedTodoevent.getTodoeventId());
            LifManageList list = lifManageListService.selectLifManageListByDeviceId(relatedTodoevent.getRelatedId());
            sysTodoevent.setReminder("请您评审"+list.getDeviceName()+"设备技术规格说明书");
            sysTodoeventService.updateSysTodoevent(sysTodoevent);
        }
        return sysDeviceStateCheckMapper.updateYesState(devId);
    }

    @Override
    public int insertSysDeviceState(SysDeviceState sysDeviceState) {
        //修改清单中技术规格说明说状态
        Long dId = sysDeviceState.getDevId();

        //获取设备清单主键id
        Long device = sysDeviceStateCheckMapper.selectFilByDId(dId);
        lifManageListService.updateLifManageListByDeviceById(device);
        //修改清单设备周期步骤
        lifManageListService.updateDevicelifeStatusByDeviceId(device);
        //修改待办事项
        RelatedTodoevent relatedTodoevent = relatedTodoeventService.selectRelatedTodoeventByDevId(device);
        if(null!=relatedTodoevent){
            SysTodoevent sysTodoevent = sysTodoeventService.selectSysTodoeventById(relatedTodoevent.getTodoeventId());
            sysTodoevent.setStatus(1L);
            sysTodoeventService.updateSysTodoeventById(sysTodoevent);
        }
        //删除待办事项中间表
        relatedTodoeventService.deleteRelatedTodoeventById(device);
        //修改设备状态确定状态：审评通过
        return sysDeviceStateCheckMapper.insertSysDeviceState(sysDeviceState);
    }

    @Override
    public Long insertPathName(SysDeviceStateFile stateFile) {
        return sysDeviceStateCheckMapper.insertPathName(stateFile);
    }

    @Override
    public int updateFileState(Long devId) {

        return lifManageListService.updateLifManageListByDeviceFlieId(devId);
    }
}
